(function () {
    const curdFilter = {
        title: "",
        body: [
            {
                type: "group",
                mode: "horizontal",//水平模式
                body: [
                    {
                        label: "模板组",
                        type: "select",
                        name: "groupCode",
                        clearable: true,
                        source: "/api/templateGroup/listSelect"
                    },
                    {
                        label: "模板编号",
                        type: "input-text",
                        name: "code",
                        clearable: true
                    },
                ]
            }
        ],
        actions: [
            {
                type: "button",
                icon: "fa fa-plus",
                tooltip: "新增模板",
                actionType: "dialog",
                reload: "crud_1",
                dialog: {
                    title: "新增模板",
                    size: "lg",
                    body: {
                        type: "form",
                        api: "post:/api/template/add",
                        body: [
                            {
                                label: "模板组",
                                type: "select",
                                name: "groupCode",
                                clearable: true,
                                source: "api/templateGroup/listSelect"
                            },
                            {
                                label: "模板编号",
                                type: "input-text",
                                name: "code",
                            }
                        ]
                    }
                }
            },
            {
                type: "reset",
                label: "重置"
            },
            {
                type: "submit",
                level: "primary",
                label: "查询"
            }
        ]
    }

    const itemActions = [
        {
            type: "button",
            icon: "fa fa-pencil",
            tooltip: "编辑",
            actionType: "dialog",
            dialog: {
                size: "lg",
                title: "",
                body: [
                    {
                        type: "form",
                        target: "form_2",
                        body: [
                            {
                                type: "group",
                                body: [
                                    {
                                        label: "数据库",
                                        type: "select",
                                        name: "dataSourceCode",
                                        clearable: true,
                                        source: "/api/dataSource/listSelect",
                                        required: true
                                    },
                                    {
                                        label: "表",
                                        type: "select",
                                        name: "tableName",
                                        clearable: true,
                                        source: "api/tableinfo/listSelect?dataSourceCode=${dataSourceCode}",
                                        initFetchOn: "data.dataSourceCode",
                                        required: true
                                    },
                                    {
                                        label: "预览",
                                        type: "submit",
                                        level: "warning"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        type: "divider"
                    },
                    {
                        type: "form",
                        name: "form_2",
                        initApi: "/api/template/findById?id=${id}&dataSourceCode=${dataSourceCode}&tableName=${tableName}",
                        api: "post:/api/template/edit/${id}",
                        body: [
                            {
                                type: "group",
                                body: [
                                    {
                                        type: "input-text",
                                        name: "groupCode",
                                        label: "模板编号",
                                        disabled: true
                                    },
                                    {
                                        type: "input-text",
                                        name: "code",
                                        label: "模板编号",
                                        disabled: true
                                    },
                                    {
                                        type: "input-text",
                                        name: "filePath",
                                        label: "文件路径"
                                    }
                                ]
                            },
                            {
                                type: "tabs",
                                tabs: [
                                    {
                                        title: "模板内容",
                                        type: "plaintext-editor",
                                        name: "content",
                                        value: "${content}"
                                    },
                                    {
                                        title: "参数",
                                        type: "json-editor",
                                        name: "paramsJson",
                                        value: "${paramsJson}"
                                    },
                                    {
                                        title: "生成文件内容",
                                        type: "plaintext-editor",
                                        name: "createFileContent",
                                        value: "${createFileContent}"
                                    }
                                ]
                            }
                        ]
                    }
                ],
                actions: [
                    {
                        type: "submit",
                        label: "保存",
                        level: "primary"
                    },
                    {
                        type: "action",
                        label: "实时预览",
                        level: "warning",
                        actionType: "dialog",
                        dialog: {
                            title: "",
                            size: "lg",
                            actions: [],
                            body: {
                                type: "form",
                                initApi: {
                                    method: "post",
                                    url: "/api/template/previewByContent",
                                    data: {
                                        id: "${id}",
                                        content: "${content}",
                                        dataSourceCode: "${dataSourceCode}",
                                        tableName: "${tableName}"
                                    }
                                },
                                body: [
                                    {
                                        type: "editor",
                                        mode: "normal",
                                        name: "createFileContent",
                                        label: "生成文件内容"
                                    }
                                ]
                            }
                        }
                    }
                ]
            }
        },
        {
            type: "button",
            icon: "fa fa-trash",
            tooltip: "删除",
            actionType: "ajax",
            confirmText: "您确认要删除模板$id吗?",
            api: "/api/template/remove/$id"
        }
    ]

    const crud = {
        type: "crud",
        name: "crud_1",
        api: "post:/api/template/findByCondition",
        orderField: "id",
        affixHeader: true,//开启固定表头功能
        filter: curdFilter,
        mode: "cards",
        card: {
            body: [
                {
                    label: "模板编号",
                    name: "code"
                },
                {
                    label: "模板组",
                    name: "groupCode"
                },
                {
                    label: "文件路径",
                    name: "filePath"
                }
            ],
            actions: itemActions
        }
    }

    const response = {
        data: {
            type: "page",
            title: "",
            body: [crud]
        },
        status: 0
    }

    window.jsonpCallback && window.jsonpCallback(response);
})();
